﻿<UserControl x:Class="XmlEditor.Presentation.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:Converters="clr-namespace:XmlEditor.Presentation.Converters"
             xmlns:Helpers="clr-namespace:XmlEditor.Presentation.Helpers"
             xmlns:Properties="clr-namespace:XmlEditor.Presentation.Properties"
             AllowDrop="True" Helpers:AttachedProperties.DropCommand="{Binding DropCommand}" >

    <UserControl.Resources>
        <DataTemplate x:Key="DocumentTypeTemplate">
            <TextBlock Text="{Binding Description}" />
        </DataTemplate>
    </UserControl.Resources>

    <UserControl.InputBindings>
        <KeyBinding x:Name="newKeyBinding"
                    Command="{Binding NewDocumentCommand}"
                    Gesture="Ctrl+N" />
        <KeyBinding x:Name="openKeyBinding"
                    Command="{Binding OpenCommand}"
                    Gesture="Ctrl+O" />
        <KeyBinding Command="{Binding DeleteNodeComand}"
                    Gesture="Ctrl+Del" />
        <KeyBinding x:Name="closeKeyBinding"
                    Command="{Binding CloseCommand}"
                    Gesture="Ctrl+F4" />
        <KeyBinding x:Name="saveKeyBinding"
                    Command="{Binding SaveCommand}"
                    Gesture="Ctrl+S" />
        <KeyBinding Command="{Binding SaveAsCommand}"
                    Gesture="Ctrl+Shift+S" />
        <KeyBinding x:Name="printKeyBinding"
                    Command="{Binding PrintCommand}"
                    Gesture="Ctrl+P" />
        <KeyBinding x:Name="printPreviewKeyBinding"
                    Command="{Binding PrintPreviewCommand}"
                    Gesture="Ctrl+Shift+P" />
        <KeyBinding x:Name="aboutKeyBinding"
                    Command="{Binding AboutCommand}"
                    Gesture="F1" />
        <KeyBinding x:Name="nextDocumentKeyBinding"
                    Command="{Binding NextDocumentCommand}"
                    Gesture="Ctrl+Tab" />
    </UserControl.InputBindings>

    <DockPanel FocusManager.IsFocusScope="True">
        <!-- Main menu -->
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="{x:Static Properties:Resources.FileMenu}">
                <MenuItem Command="{Binding NewDocumentCommand}"
                          Header="{x:Static Properties:Resources.NewFromXsdMenu}"
                          Icon="{StaticResource NewImage}"
                          InputGestureText="{x:Static Properties:Resources.ControlN}" />
                <MenuItem Header="{x:Static Properties:Resources.NewMenu}"
                          ItemsSource="{Binding DocumentTypes}"
                          ItemTemplate="{StaticResource DocumentTypeTemplate}">
                    <MenuItem.ItemContainerStyle>
                        <Style>
                            <Setter Property="MenuItem.Command"
                                    Value="{Binding RelativeSource={RelativeSource AncestorType=Menu}, Path=DataContext.NewXsdCacheBasedDocumentCommand}" />
                            <Setter Property="MenuItem.CommandParameter"
                                    Value="{Binding}" />
                        </Style>
                    </MenuItem.ItemContainerStyle>
                </MenuItem>
                <MenuItem Command="{Binding OpenCommand}"
                          Header="{x:Static Properties:Resources.OpenMenu}"
                          Icon="{StaticResource OpenImage}"
                          InputGestureText="{x:Static Properties:Resources.ControlO}" />
                <MenuItem x:Name="closeMenuItem"
                          Command="{Binding CloseCommand}"
                          Header="{x:Static Properties:Resources.CloseMenu}" />
                <MenuItem Command="{Binding SaveCommand}"
                          Header="{x:Static Properties:Resources.SaveMenu}"
                          Icon="{StaticResource SaveImage}"
                          InputGestureText="{x:Static Properties:Resources.ControlS}" />
                <MenuItem Command="{Binding SaveAsCommand}"
                          Header="{x:Static Properties:Resources.SaveAsMenu}" />
                <Separator />
                <MenuItem Command="{Binding PrintCommand}"
                          Header="{x:Static Properties:Resources.PrintMenu}"
                          Icon="{StaticResource PrintImage}"
                          InputGestureText="{x:Static Properties:Resources.ControlP}" />
                <MenuItem Command="{Binding PrintPreviewCommand}"
                          Header="{x:Static Properties:Resources.PrintPreviewMenu}"
                          Icon="{StaticResource PrintPreviewImage}" />
                <Separator />
                <MenuItem Header="Recent..."
                          IsEnabled="{Binding MRU.Count, Converter={x:Static Converters:ConvertIntegerToBool.Default}, ConverterParameter='0'}"
                          ItemsSource="{Binding MRU}">
                    <MenuItem.ItemContainerStyle>
                        <Style>
                            <Setter Property="MenuItem.Command"
                                    Value="{Binding RelativeSource={RelativeSource AncestorType=Menu}, Path=DataContext.OpenCommand}" />
                            <Setter Property="MenuItem.CommandParameter"
                                    Value="{Binding}" />
                        </Style>
                    </MenuItem.ItemContainerStyle>
                </MenuItem>
                <Separator />
                <MenuItem Command="{Binding ExitCommand}"
                          Header="{x:Static Properties:Resources.ExitMenu}" />
            </MenuItem>
            <MenuItem Header="{x:Static Properties:Resources.EditMenu}">
                <MenuItem Command="ApplicationCommands.Undo" Header="{x:Static Properties:Resources.UndoMenu}" Icon="{StaticResource UndoImage}"/>
                <MenuItem Command="ApplicationCommands.Redo" Header="{x:Static Properties:Resources.RedoMenu}" Icon="{StaticResource RedoImage}"/>
                <Separator/>
                <MenuItem Command="ApplicationCommands.Cut"
                          Header="{x:Static Properties:Resources.CutMenu}"
                          Icon="{StaticResource CutImage}" />
                <MenuItem Command="ApplicationCommands.Copy"
                          Header="{x:Static Properties:Resources.CopyMenu}"
                          Icon="{StaticResource CopyImage}" />
                <MenuItem Command="ApplicationCommands.Paste"
                          Header="{x:Static Properties:Resources.PasteMenu}"
                          Icon="{StaticResource PasteImage}" />
                <MenuItem Command="EditingCommands.Delete"
                          Header="{x:Static Properties:Resources.DeleteMenu}"
                          Icon="{StaticResource DeleteImage}" />
                <Separator />
                <MenuItem Command="ApplicationCommands.SelectAll"
                          Header="{x:Static Properties:Resources.SelectAllMenu}" />
            </MenuItem>
            <MenuItem Header="{x:Static Properties:Resources.ToolsMenu}">
                <MenuItem Header="{x:Static Properties:Resources.LanguageMenu}">
                    <MenuItem Command="{Binding EnglishCommand}"
                              Header="{x:Static Properties:Resources.EnglishMenu}"
                              Icon="{StaticResource FlagEnImage}" />
                    <MenuItem Command="{Binding GermanCommand}"
                              Header="{x:Static Properties:Resources.GermanMenu}"
                              Icon="{StaticResource FlagDeImage}" />
                </MenuItem>
            </MenuItem>
            <MenuItem Header="{x:Static Properties:Resources.HelpMenu}">
                <MenuItem Command="{Binding AboutCommand}"
                          Header="{x:Static Properties:Resources.AboutMenu}" />
            </MenuItem>
        </Menu>

        <!--Toolbar menu-->
        <ToolBarTray DockPanel.Dock="Top"
                     FocusManager.IsFocusScope="True">
            <ToolBar>
                <Button Command="{Binding NewDocumentCommand}"
                        Content="{StaticResource NewImage}"
                        ToolTip="{x:Static Properties:Resources.New}" />
                <Button Command="{Binding OpenCommand}"
                        Content="{StaticResource OpenImage}"
                        ToolTip="{x:Static Properties:Resources.Open}" />
                <Button Command="{Binding SaveCommand}"
                        Content="{StaticResource SaveImage}"
                        ToolTip="{x:Static Properties:Resources.Save}" />
                <Button Command="{Binding PrintCommand}"
                        Content="{StaticResource PrintImage}"
                        ToolTip="{x:Static Properties:Resources.Print}" />
                <Button Command="{Binding PrintPreviewCommand}"
                        Content="{StaticResource PrintPreviewImage}"
                        ToolTip="{x:Static Properties:Resources.PrintPreview}" />
                <Separator />
                <!--<Button Command="{Binding ActiveDocumentView.DataContext.UndoCommand}" Content="{StaticResource UndoImage}" ToolTip="{x:Static Properties:Resources.Undo}"/>
                <Button Command="Redo" Content="{StaticResource RedoImage}" ToolTip="{x:Static Properties:Resources.Redo}"/>-->
                <Button Command="ApplicationCommands.Undo"
                        Content="{StaticResource UndoImage}"
                        ToolTip="{x:Static Properties:Resources.Undo}" />
                <Button Command="ApplicationCommands.Redo"
                        Content="{StaticResource RedoImage}"
                        ToolTip="{x:Static Properties:Resources.Redo}" />
                <Button Command="ApplicationCommands.Cut"
                        Content="{StaticResource CutImage}"
                        ToolTip="{x:Static Properties:Resources.Cut}" />
                <Button Command="ApplicationCommands.Copy"
                        Content="{StaticResource CopyImage}"
                        ToolTip="{x:Static Properties:Resources.Copy}" />
                <Button Command="ApplicationCommands.Paste"
                        Content="{StaticResource PasteImage}"
                        ToolTip="{x:Static Properties:Resources.Paste}" />
                <AdornerDecorator>
                    <TextBox x:Name="Search"
                             Margin="5 0"
                             Width="150"
                             Text="{Binding SearchTerm}"
                             VerticalAlignment="Center"
                             Helpers:InputBindingsManager.UpdatePropertySourceWhenEnterPressed="TextBox.Text">
                        <Helpers:WatermarkService.Watermark>
                            <StackPanel Orientation="Horizontal">
                                <Image Height="16"
                                       Margin="5 0"
                                       Source="/eXed;component/Resources/Images/find.png" />
                                <!--<TextBlock Text="Search"
                                       FontStyle="Italic"
                                       VerticalAlignment="Center" />-->
                            </StackPanel>
                        </Helpers:WatermarkService.Watermark>
                    </TextBox>
                </AdornerDecorator>
                <!--<TextBox Text="{Binding SearchTerm, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="150" />-->
            </ToolBar>
        </ToolBarTray>

        <!--Documents tab-->
        <TabControl x:Name="Documents"
                    Focusable="True"
                    ItemsSource="{Binding DocumentViews}"
                    SelectedItem="{Binding ActiveDocumentView}">
            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="Header"
                            Value="{Binding Content.DataContext}" />
                    <Setter Property="HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock VerticalAlignment="Center">
                                        <TextBlock.Text>
                                            <MultiBinding Converter="{x:Static Converters:FileNameConverter.Default}">
                                                <Binding Path="Document.FileName" />
                                                <Binding Path="Document.Modified" />
                                            </MultiBinding>
                                        </TextBlock.Text>
                                    </TextBlock>
                                    <Button Height="16"
                                            Margin="7,0,0,0"
                                            Padding="0"
                                            Width="16"
                                            BorderThickness="0"
                                            Command="{Binding Command, ElementName=closeMenuItem}"
                                            Content="{StaticResource CloseSmallImage}"
                                            MinHeight="0"
                                            MinWidth="0"
                                            Visibility="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}},                                                 Converter={x:Static Converters:BoolToVisibilityConverter.Default}}">
                                        <Button.Template>
                                            <ControlTemplate TargetType="{x:Type Button}">
                                                <ContentPresenter Margin="{TemplateBinding Control.Padding}"
                                                                  Content="{TemplateBinding ContentControl.Content}"
                                                                  ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                                                                  HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                                                                  RecognizesAccessKey="True"
                                                                  SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
                                                                  VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" />
                                            </ControlTemplate>
                                        </Button.Template>
                                    </Button>
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.ItemContainerStyle>
        </TabControl>
    </DockPanel>
</UserControl>